package business.search_eng;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class ResultList {
	
	public ArrayList<ResultListItem> documents;
	
	public ResultList () {
		this.documents = new ArrayList<ResultListItem>();
	}
	
	public void addDocument(ResultListItem id) {
		this.documents.add(id);
	}
	
	public void addDocument(String documentId, Double score) {
		addDocument( new ResultListItem(documentId, score) );
	}
	
	public void sortByScore() {
		try {
			Collections.sort(this.documents);
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	
	public String JSONstringify() {
		StringBuilder sb = new StringBuilder();
		Iterator<ResultListItem> it = this.documents.iterator();
		sb.append("[");
		boolean first = true;
		while (it.hasNext()) {
			if (first) {first= false;}
			else {sb.append(",");}
			sb.append ("'"+it.next().documentId+"'");
		}
		sb.append("]");
		return sb.toString();
	}

}
